home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / Libraries / MacPNG Library 1.02 / pngMacSrc 1.02 / PNG Library 0.80 / PNG.C < prev    next >
Encoding:
C/C++ Source or Header  |  1996-05-29  |  5.8 KB  |  207 lines  |  [TEXT/CWIE]

  1.  
  2. /* png.c - location for general purpose png functions
  3.  
  4.    libpng 1.0 beta 2 - version 0.8
  5.    For conditions of distribution and use, see copyright notice in png.h
  6.    Copyright (c) 1995 Guy Eric Schalnat, Group 42, Inc.
  7.    August 20, 1995
  8.    */
  9.  
  10. #define PNG_INTERNAL
  11. #define PNG_NO_EXTERN
  12. #include "png.h"
  13.  
  14. /* version information for c files.  This better match the version
  15.    string defined in png.h */
  16. char png_libpng_ver[] = "0.80";
  17.  
  18. /* place to hold the signiture string for a png file. */
  19. png_byte png_sig[8] = {137, 80, 78, 71, 13, 10, 26, 10};
  20.  
  21. /* constant strings for known chunk types.  If you need to add a chunk,
  22.    add a string holding the name here.  If you want to make the code
  23.    portable to EBCDIC machines, use ASCII numbers, not characters. */
  24. png_byte png_IHDR[4] = { 73,  72,  68,  82};
  25. png_byte png_IDAT[4] = { 73,  68,  65,  84};
  26. png_byte png_IEND[4] = { 73,  69,  78,  68};
  27. png_byte png_PLTE[4] = { 80,  76,  84,  69};
  28. #if defined(PNG_READ_gAMA_SUPPORTED) || defined(PNG_WRITE_gAMA_SUPPORTED)
  29. png_byte png_gAMA[4] = {103,  65,  77,  65};
  30. #endif
  31. #if defined(PNG_READ_sBIT_SUPPORTED) || defined(PNG_WRITE_sBIT_SUPPORTED)
  32. png_byte png_sBIT[4] = {115,  66,  73,  84};
  33. #endif
  34. #if defined(PNG_READ_cHRM_SUPPORTED) || defined(PNG_WRITE_cHRM_SUPPORTED)
  35. png_byte png_cHRM[4] = { 99,  72,  82,  77};
  36. #endif
  37. #if defined(PNG_READ_tRNS_SUPPORTED) || defined(PNG_WRITE_tRNS_SUPPORTED)
  38. png_byte png_tRNS[4] = {116,  82,  78,  83};
  39. #endif
  40. #if defined(PNG_READ_bKGD_SUPPORTED) || defined(PNG_WRITE_bKGD_SUPPORTED)
  41. png_byte png_bKGD[4] = { 98,  75,  71,  68};
  42. #endif
  43. #if defined(PNG_READ_hIST_SUPPORTED) || defined(PNG_WRITE_hIST_SUPPORTED)
  44. png_byte png_hIST[4] = {104,  73,  83,  84};
  45. #endif
  46. #if defined(PNG_READ_tEXt_SUPPORTED) || defined(PNG_WRITE_tEXt_SUPPORTED)
  47. png_byte png_tEXt[4] = {116,  69,  88, 116};
  48. #endif
  49. #if defined(PNG_READ_zTXt_SUPPORTED) || defined(PNG_WRITE_zTXt_SUPPORTED)
  50. png_byte png_zTXt[4] = {122,  84,  88, 116};
  51. #endif
  52. #if defined(PNG_READ_pHYs_SUPPORTED) || defined(PNG_WRITE_pHYs_SUPPORTED)
  53. png_byte png_pHYs[4] = {112,  72,  89, 115};
  54. #endif
  55. #if defined(PNG_READ_oFFs_SUPPORTED) || defined(PNG_WRITE_oFFs_SUPPORTED)
  56. png_byte png_oFFs[4] = {111,  70,  70, 115};
  57. #endif
  58. #if defined(PNG_READ_tIME_SUPPORTED) || defined(PNG_WRITE_tIME_SUPPORTED)
  59. png_byte png_tIME[4] = {116,  73,  77,  69};
  60. #endif
  61.  
  62. /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
  63.  
  64. /* start of interlace block */
  65. int png_pass_start[] = {0, 4, 0, 2, 0, 1, 0};
  66.  
  67. /* offset to next interlace block */
  68. int png_pass_inc[] = {8, 8, 4, 4, 2, 2, 1};
  69.  
  70. /* start of interlace block in the y direction */
  71. int png_pass_ystart[] = {0, 0, 4, 0, 2, 0, 1};
  72.  
  73. /* offset to next interlace block in the y direction */
  74. int png_pass_yinc[] = {8, 8, 8, 4, 4, 2, 2};
  75.  
  76. /* width of interlace block */
  77. /* this is not currently used - if you need it, uncomment it here and
  78.    in png.h
  79. int png_pass_width[] = {8, 4, 4, 2, 2, 1, 1};
  80. */
  81.  
  82. /* height of interlace block */
  83. /* this is not currently used - if you need it, uncomment it here and
  84.    in png.h
  85. int png_pass_height[] = {8, 8, 4, 4, 4, 2, 2, 1};
  86. */
  87.  
  88. /* mask to determine which pixels are valid in a pass */
  89. int png_pass_mask[] = {0x80, 0x08, 0x88, 0x22, 0xaa, 0x55, 0xff};
  90.  
  91. /* mask to determine which pixels to overwrite while displaying */
  92. int png_pass_dsp_mask[] = {0xff, 0x0f, 0xff, 0x33, 0xff, 0x55, 0xff};
  93.  
  94.  
  95. int
  96. png_check_sig(png_byte *sig, int num)
  97. {
  98.    if (num > 8)
  99.       num = 8;
  100.    if (num < 1)
  101.       return 0;
  102.  
  103.    return (!memcmp(sig, png_sig, num));
  104. }
  105.  
  106. /* Function to allocate memory for zlib. */
  107. voidp
  108. png_zalloc(voidp png_ptr, uInt items, uInt size)
  109. {
  110.    return ((voidp)png_large_malloc((png_struct *)png_ptr,
  111.       (png_uint_32)items * (png_uint_32)size));
  112. }
  113.  
  114. /* function to free memory for zlib */
  115. void
  116. png_zfree(voidp png_ptr, voidp ptr)
  117. {
  118.    png_large_free((png_struct *)png_ptr, (void *)ptr);
  119. }
  120.  
  121. /* reset the crc variable to 32 bits of 1's.  Care must be taken
  122.    in case crc is > 32 bits to leave the top bits 0 */
  123. void
  124. png_reset_crc(png_struct *png_ptr)
  125. {
  126.    /* set crc to all 1's */
  127.    png_ptr->crc = 0xffffffffL;
  128. }
  129.  
  130. /* Note: the crc code below was copied from the sample code in the
  131.    PNG spec, with appropriate modifications made to ensure the
  132.    variables are large enough */
  133.  
  134. /* table of crc's of all 8-bit messages.  If you wish to png_malloc this
  135.    table, turn this into a pointer, and png_malloc it in make_crc_table().
  136.    You may then want to hook it into png_struct and free it with the
  137.    destroy functions. */
  138. static png_uint_32 crc_table[256];
  139.  
  140. /* Flag: has the table been computed? Initially false. */
  141. static int crc_table_computed = 0;
  142.  
  143. /* make the table for a fast crc */
  144. static void
  145. make_crc_table(void)
  146. {
  147.   png_uint_32 c;
  148.   int n, k;
  149.  
  150.   for (n = 0; n < 256; n++)
  151.   {
  152.    c = (png_uint_32)n;
  153.    for (k = 0; k < 8; k++)
  154.      c = c & 1 ? 0xedb88320L ^ (c >> 1) : c >> 1;
  155.    crc_table[n] = c;
  156.   }
  157.   crc_table_computed = 1;
  158. }
  159.  
  160. /* update a running crc with the bytes buf[0..len-1]--the crc should be
  161.    initialized to all 1's, and the transmitted value is the 1's complement
  162.    of the final running crc. */
  163. static png_uint_32
  164. update_crc(png_uint_32 crc, png_byte *buf, png_uint_32 len)
  165. {
  166.   png_uint_32 c;
  167.   png_byte *p;
  168.   png_uint_32 n;
  169.  
  170.   c = crc;
  171.   p = buf;
  172.   n = len;
  173.  
  174.   if (!crc_table_computed)
  175.   {
  176.    make_crc_table();
  177.   }
  178.  
  179.   if (n > 0) do
  180.   {
  181.    c = crc_table[(png_byte)((c ^ (*p++)) & 0xff)] ^ (c >> 8);
  182.   } while (--n);
  183.  
  184.   return c;
  185. }
  186.  
  187. /* calculate the crc over a section of data.  Note that while we
  188.    are passing in a 32 bit value for length, on 16 bit machines, you
  189.    would need to use huge pointers to access all that data.  If you
  190.    need this, put huge here and above. */
  191. void
  192. png_calculate_crc(png_struct *png_ptr, png_byte *ptr,
  193.    png_uint_32 length)
  194. {
  195.    png_ptr->crc = update_crc(png_ptr->crc, ptr, length);
  196. }
  197.  
  198. /* initialize the info structure */
  199. void
  200. png_info_init(png_info *info)
  201. {
  202.    /* set everything to 0 */
  203.    memset(info, 0, sizeof (png_info));
  204. }
  205.  
  206.  
  207.